#!/usr/bin/env python
# coding:utf8

import os
import time

def execute(list):
    for k in list:
        # mkdir是截图文件存放的路径，可以修改
        mkdir = '/tmp/screenshosts/' + time.strftime('%Y%m', time.localtime(time.time())) + \
                '/' + time.strftime('%Y%m%d', time.localtime(time.time())) + '/' + \
                k[:12] + '/'
        if not os.path.exists(mkdir):
            #创建截图文件存放的目录
            os.makedirs(mkdir)
            # 创建截图文件备份目录
            os.makedirs(mkdir+'back')
        now_time = time.strftime('%Y%m%d%H%M%S',time.localtime(time.time()))
        #copy'now.jpg'到备份目录下
        comm = 'cp ' + mkdir +'now.jpg ' + mkdir + 'back/' + now_time + '.jpg'
        #copy容器的'now.jpg'到指定截图存放目录下
        comm_1 = 'cp -f ' + '/var/lib/docker/aufs/mnt/' + k + '/tmp/screenshots/now.jpg ' + mkdir
        os.system(comm)
        os.system(comm_1)


def jietu():
    list = []
    ID = os.popen("docker inspect --format '{{.Id}}'  $(docker ps -q)")
    for Id in ID:
        temp = str(Id).replace('\r', '').replace('\n', '')
        list.append(temp)
        #将所有ID值存到list中
    execute(list)

#if __name__ == "__main__":
while True:
    jietu()
    time.sleep(11)
